<?php
namespace addons\vote\controllers;
use system\Base;

class rank extends Base{
	protected static $key = 'users';
	/**
	 * @var \Redis $redis
	 */
	protected static $redis = '';

	/**
	 * 添加分数
	 * @param int $member_id
	 * @param int $ticket
	 * @author Colin <amcolin@126.com>
	 * @date 2022-02-13 下午6:38
	 */
	public static function addScore($member_id = 0 , $ticket = 0){
		if ($member_id > 0 && $ticket > 0){
			self::getRedis();
			self::$redis->zIncrBy(self::$key , $ticket , $member_id);
		}
	}

	/**
	 * 添加分数
	 * @param int $member_id
	 * @param int $ticket
	 * @author Colin <amcolin@126.com>
	 * @date 2022-02-13 下午6:38
	 */
	public static function setScore($member_id = 0 , $ticket = 0){
		if ($member_id > 0 && $ticket > 0){
			self::getRedis();
			self::$redis->zAdd(self::$key , $ticket , $member_id);
		}
	}

	/**
	 * 获取排名
	 * @param $member_id
	 * @return bool|int
	 * @author Colin <amcolin@126.com>
	 * @date 2022-02-13 下午6:38
	 */
	public static function getRank($member_id){
		self::getRedis();
		$score = self::$redis->zRevRank(self::$key , $member_id);
		if ($score === false){
			return 0;
		}
		return $score + 1;
	}

	/**
	 * 获取Redis
	 * @author Colin <amcolin@126.com>
	 * @date 2022-02-13 下午6:42
	 */
	protected static function getRedis(){
		if (!self::$redis){
			self::$redis = getRedis();
		}
	}
}